<form id="adduser-form" lay-filter="adduser-form" class="layui-form model-form fromzby">
	<div class="layui-form-item">
		<label class="layui-form-label">推送标题</label>
		<div class="layui-input-block">
			<input name="txName" placeholder="请输入标题" type="text" class="layui-input" maxlength="20" lay-verify="required" required/>
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">推送内容</label><br />
		<div class="layui-input-block">
			<textarea name="txnei" placeholder="请输入推送内容，100字以内" class="layui-textarea" lay-verify="required" required></textarea>
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">选中成员</label>
		<div class="layui-input-block">
			<select name="label" xm-select="label" lay-verify="required">
			</select>
		</div>
	</div>

	<div class="layui-form-item model-form-footer">
		<button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
		<button class="layui-btn" lay-filter="adduser-form-submit" lay-submit>保存</button>
	</div>
</form>

<script>
	layui.use(['layer', 'admin', 'form', 'formSelects', 'config'], function() {
		var layer = layui.layer;
		var admin = layui.admin;
		var form = layui.form;
		var formSelects = layui.formSelects;
		var config = layui.config;
		var alog = config.getTempData('loginInfo');

		var sUser = admin.getTempData('selectUser2');

		if(0 != sUser.length) {
			// 渲染多选下拉框
			var roleSelectData = new Array();
			for(var i = 0; i < sUser.length; i++) {
				roleSelectData.push({
					name: sUser[i].nickname,
					value: sUser[i].userId + '+' + sUser[i].jiguangId
				});
			}
			console.log(roleSelectData)
			formSelects.data('label', 'local', {
				arr: roleSelectData
			});

			var rdss = new Array();

			for(var i = 0; i < sUser.length; i++) {
				rdss.push(sUser[i].userId + '+' + sUser[i].jiguangId);
			}

			console.log(rdss)
			formSelects.value('label', rdss);

		} else {
			layer.msg('获取角色失败', {
				icon: 2
			});
		}

		// 表单提交事件
		form.on('submit(adduser-form-submit)', function(data) {
			console.log(data)

			var addArrMember = [];

			var selusers = data.field.label;

			var addM = "["

			selusers = selusers.split(",");
			for(var i = 0; i < selusers.length; i++) {
				var x = selusers[i].split("+");
				console.log("选择", x)
				if(x != null) {
					if(i == selusers.length - 1) {
						addM += '{"jiguangId":"' + x[1] + '","userid":"' + x[0] + '"}'
					} else {
						addM += '{"jiguangId":"' + x[1] + '","userid":"' + x[0] + '"},'
					}
				}
			}
			addM += "]"

			addM = JSON.parse(addM);

			console.log(addM)

			var User = function(userId, title, content, jiguangId, type) {
				this.userId = userId;
				this.title = title;
				this.content = content;
				this.jiguangId = jiguangId;
				this.type = type;
			};
			User.create = function(userId, title, content, jiguangId, type) {
				return new User(userId, title, content, jiguangId, type);
			};

			addM.forEach(function(item, index) {
				addArrMember.push(User.create(item.userid, data.field.txName, data.field.txnei, item.jiguangId, 2));
			})
			console.log(addArrMember)

			$.ajax({
				type: "POST",
				url: sendPush,
				data: {
					params: JSON.stringify(addArrMember)
				},
				dataType: "json",
				xhrFields: {
					withCredentials: true
				},
				success: function(data) {
					layer.closeAll();
					if(data.statusCode == 0) {
						layer.msg("发送成功", {
							icon: 1
						});
					}else{
						layer.msg("发送失败", {
							icon: 2
						});
					}
					admin.finishPopupCenter();
					$('#adduser-form').attr('method')
				},
				error: function(result) {
					console.log(result);
				},
			});
			//			
			return false;
		});
	});
</script>